package com.strebel.meal;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.strebel.meal.data.Meal;
import com.strebel.meal.data.MealImpl;

public class CreateActivity extends Activity {

	private Meal meal;
	private List<EditText> editList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create);

		meal = new MealImpl();
		editList = new LinkedList<EditText>();
		
		addIngred();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && data != null) {
			// Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
			Uri selectedImageUri = data.getData();
			String selectedImagePath = getPath(selectedImageUri);
			
			Bitmap thumbnail = BitmapFactory.decodeFile(selectedImagePath);

			Display display = getWindowManager().getDefaultDisplay();
			Point size = new Point();
			display.getSize(size);
			int dstWidth = size.x;
			int dstHeight = size.y;
			
			thumbnail = Bitmap.createScaledBitmap(thumbnail, dstWidth, dstHeight, false);
			
			meal.setImage(selectedImagePath);

			ImageView imageView = (ImageView) findViewById(R.id.createMealImage);
			imageView.setImageURI(selectedImageUri);
		}
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_create, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_save:
			saveMeal();
			Intent intent = new Intent(this, MainActivity.class);
			intent.putExtra("data", new MealImpl(meal));
			startActivity(intent);
			break;
		case R.id.menu_add_ingred:
			addIngred();
			break;
		case R.id.menu_picture:
			takePicture();
			break;
		}

		return true;
	}

	private void addIngred() {
		EditText ingred = new EditText(this);
		editList.add(ingred);
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.createLayout);
		layout.addView(ingred);
		
		ingred.requestFocus();
	}

	private void takePicture() {
		// Intent intent = new
		// Intent("android.media.action.IMAGE_CAPTURE");
		
		Intent intent = new Intent(
				Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(intent, 0);
	}

	private void saveMeal() {
		EditText editText = (EditText) findViewById(R.id.createEditTitle);

		meal.setTitle(editText.getText().toString());
		for(EditText ingredEditText : editList) {
			meal.addIngredient(ingredEditText.getText().toString());
		}
	}
}
